app.controller('BusShiftListCtrl', function ($scope, $modal, ajax, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });

	//状态
	$scope.statusList = [
		{ id: 0, name: "等待发布" },
		{ id: 1, name: "等待报班" },
		{ id: 2, name: "等待发班" },
		{ id: 3, name: "运行中" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "晚点" },
		{ id: 6, name: "塌班" },
		{ id: 7, name: "被取消" },
	];
	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.routeList = [];

	$scope.reload = $scope.page.loader('shift/bs/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.departDate = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.departDate;
		}
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.routeList.selected != undefined) {
			$scope.queryMap.route__id = $scope.routeList.selected.id;
		} else if ($scope.routeList.selected == undefined) {
			delete $scope.queryMap.route__id;
		}
		$scope.reload(1, $scope.queryMap);
	};
	$scope.query();

	//获取线路列表
	$scope.loadRouteList = function () {
		ajax.call(new Req.GET({
			resource: 'route/bs/list'
		})).then(function (result) {
			$scope.routeList = result.result;
		}, function () { });
	};
	$scope.loadRouteList();

	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		$modal.open({
			templateUrl: 'template/bus/shift/writeModal.html?v=194ff1e836',
			controller: 'BusShiftModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		}).result.then(function (param) {
			if (param.type == 'update') {
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'shift/bs/update';
				$scope.page.mod(updater, $scope.reload, $scope.queryMap);
			}
		});
	};

	$scope.updateStation = function (shift) {
		$scope.current_data = angular.copy(shift);
		$modal.open({
			templateUrl: 'template/bus/shift/updateStation.html?v=ad33703681',
			controller: 'BusShiftStaionModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": "update"
					};
				}
			}
		}).result.then(function (param) {
			updater.datalist = param;
			updater.id = shift.id;
			updater.resource = 'shift/bs/station/' + shift.id + '/update';
			ajax.call(updater).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '屏蔽站点成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '连接错误，请求失败！');
			});
		});
	};

	$scope.updatePrice = function (shift) {
		$scope.current_data = angular.copy(shift);
		$modal.open({
			templateUrl: 'template/bus/shift/updatePrice.html?v=c79a2905fd',
			controller: 'BusShiftPriceModalCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": "update"
					};
				}
			}
		}).result.then(function (param) {
			updater.fields = param.data;
			updater.id = param.data.id;
			updater.resource = 'shift/bs/station/update';
			$scope.page.mod(updater, $scope.reload, $scope.queryMap);
		});
	};


	//删除
	$scope.del = function (size, current_data, submit_type) {
		$scope.delModal(size, current_data, submit_type).result.then(function (param) {
			deleter.id = param.data.id;
			deleter.resource = 'shift/bs/' + deleter.id + '/delete';
			$scope.page.del(deleter, $scope.reload, $scope.queryMap);
		});
	};

	$scope.pause = function (shift) {
		$modal.open({
			templateUrl: 'template/bus/shift/paused.html?v=753d877096',
			controller: 'ShiftPausedModalCtrl',
			size: 'sm',
			resolve: {
				param: function () {
					return {
						"data": shift
					};
				}
			}
		}).result.then(function (param) {
			ajax.post('shift/bs/' + shift.id + '/pause', null, function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '暂停班次成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
				$scope.query();
			}, function () { toaster.pop('error', '错误', '暂停班次失败。'); });
		});
	}

	$scope.delayed = function (shift) {
		$modal.open({
			templateUrl: 'template/bus/shift/delayedShift.html?v=9d5ffc36f3',
			controller: 'BusDelayedShiftCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": shift
					};
				}
			}
		}).result.then(function (param) {
			updater.fields = param.data;
			updater.id = param.data.id;
			updater.resource = 'shift/bs/delayed';
			ajax.call(updater).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '修改发班时间成功，系统会自动给已购票的乘客发送通知。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
				$scope.query();
			}, function () {
				toaster.pop('error', '错误', '连接错误，请求失败！');
			});
		});
	};

	$scope.copy = function (shift) {
		$modal.open({
			templateUrl: 'template/bus/shift/copyShift.html?v=b6eece0c84',
			controller: 'BusCopyShiftCtrl',
			size: 'lg',
			resolve: {
				param: function () {
					return {
						"data": shift
					};
				}
			}
		}).result.then(function (param) {
			inserter.fields = param.data;
			inserter.id = param.data.id;
			inserter.resource = 'shift/bs/copy';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '创建临时班次成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
				$scope.reload(1, $scope.queryMap);
			}, function () {
				toaster.pop('error', '错误', '连接错误，请求失败！');
			});
		});
	}
});
